﻿// -----------------------------------------------------------------------
// <copyright file="Pluralizer.cs" company="Moveax, LLC">
// Copyright (c) 2013 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

namespace Moveax.Pluralization.Core
{
    using System;
    using System.Globalization;
    using System.Threading;

    /// <summary>Retrieves the pluralized and localized string from the specified source.</summary>
    public class Pluralizer
    {
        private readonly PluralRulesFactory _rulesFactory = new PluralRulesFactory();
        private readonly ILocalizedStringsSource _stringSource;
        private CultureInfo _culture;
        private IPluralRules _rules;

        /// <summary>Gets or sets the culture for which the strings will be localized and pluralized.</summary>
        /// <value>The culture for which the strings will be localized and pluralized.</value>
        /// <exception cref="System.ArgumentNullException">cultureInfo</exception>
        public CultureInfo Culture
        {
            get { return this._culture; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                if (!value.Equals(this._culture)) {
                    this._culture = value;
                    this._rules = this._rulesFactory.Create(value);
                }
            }
        }

        /// <summary>Initializes a new instance of the <see cref="Pluralizer"/> class.</summary>
        /// <param name="stringSource">The strings source.</param>
        public Pluralizer(ILocalizedStringsSource stringSource)
            : this(Thread.CurrentThread.CurrentCulture, stringSource)
        {
        }

        /// <summary>Initializes a new instance of the <see cref="Pluralizer"/> class.</summary>
        /// <param name="cultureInfo">Represents the culture for which the strings will be localized.</param>
        /// <param name="stringSource">The strings source.</param>
        public Pluralizer(CultureInfo cultureInfo, ILocalizedStringsSource stringSource)
        {
            this.Culture = cultureInfo;
            this._stringSource = stringSource;
        }

        /// <summary>Gets the pluralized and localized string by name.</summary>
        /// <param name="name">Name of the string.</param>
        /// <param name="value">The value.</param>
        /// <returns>The pluralized and localized string.</returns>
        public string Pluralize(string name, int value)
        {
            PluralCategory category = this._rules.GetCategoryFor(value);
            return this._stringSource.GetString(name, category, this._culture);
        }

        /// <summary>Gets the pluralized and localized string by name.</summary>
        /// <param name="name">Name of the string.</param>
        /// <param name="value">The value.</param>
        /// <returns>The pluralized and localized string.</returns>
        public string Pluralize(string name, float value)
        {
            PluralCategory category = this._rules.GetCategoryFor(value);
            return this._stringSource.GetString(name, category, this._culture);
        }
    }
}